#!/usr/bin/perl
require "cgi-lib.pl";
$homedir_tobesave = "/var/spool/news/htdocs/tlcplus";

###&ReadParse(*cgi);
&parse_form;

$results = &PrintVariables(%cgi);
sub parse_form {

   # Get the input
   read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

   # Split the name-value pairs
   @pairs = split(/&/, $buffer);

   foreach $pair (@pairs) {
      ($name, $value) = split(/=/, $pair);

      $value =~ tr/+/ /;
      $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

      $cgi{$name} = $value;
   }
}
# load in the form contents
($nickname = $cgi{'nickname'}) =~ s/\n/ /g;
($sex = $cgi{'sex'}) =~ s/\n/ /g;
($seeking = $cgi{'seeking'}) =~ s/\n/ /g;
($age = $cgi{'age'}) =~ s/\n/ /g;
($weight = $cgi{'weight'}) =~ s/\n/ /g;
($height = $cgi{'height'}) =~ s/\n/ /g;
($haircolour = $cgi{'haircolour'}) =~ s/\n/ /g;
($eyecolour = $cgi{'eyecolour'}) =~ s/\n/ /g;
($education = $cgi{'education'}) =~ s/\n/ /g;
($occupation = $cgi{'occupation'}) =~ s/\n/ /g;
($hobbies = $cgi{'hobbies'}) =~ s/\n/ /g;
($foods = $cgi{'foods'}) =~ s/\n/ /g;
($films = $cgi{'films'}) =~ s/\n/ /g;
($vacations = $cgi{'vacations'}) =~ s/\n/ /g;
($music = $cgi{'music'}) =~ s/\n/ /g;
($tv = $cgi{'tv'}) =~ s/\n/ /g;
($books = $cgi{'books'}) =~ s/\n/ /g;
($sports = $cgi{'sports'}) =~ s/\n/ /g;
($pastimes = $cgi{'pastimes'}) =~ s/\n/ /g;
($email = $cgi{'email'}) =~ s/\n/ /g;
($comments = $cgi{'comments'}) =~ s/\n/ /g;
($fullname = $cgi{'fullname'}) =~ s/\n/ /g;
($address = $cgi{'address'}) =~ s/\n/ /g;
($phone = $cgi{'phone'}) =~ s/[\r\n]/ /g;
@nick_c = split('',$nickname);
@name_c = split('',$fullname);
@day = split(/ /,localtime);
@yy = split('',@day[$#day]);
@newtime = localtime(time);
$news = (@newtime[5]*10000)+(@newtime[4]*100)+@newtime[3];
$fil = join('',@nick_c[0..2],@name_c[0..2],$news,".html");
$fil =~ tr/A-Z/a-z/;
$ref = $cgi{'refnumber'};
$emailflag = 0;
if( $email =~ /\@/ ){
	$emailflag = 1;
}
# do any error checking here
if(length($nickname) < 1){
        &myerror("You didn't put in your first name.\n");
} elsif($age < 18){
        &myerror("Age must be greater than 18.\n");
} elsif(length($sex) < 1){
        &myerror("You didn't put in your sex.\n");
} elsif(length($seeking) < 1){
        &myerror("You didn't put in who you're seeking.\n");
} elsif(length($age) < 1){
        &myerror("You didn't put in your age.\n");
} elsif(length($weight) < 1){
        &myerror("You didn't put in your weight.\n");
} elsif(length($height) < 1){
        &myerror("You didn't put in your height.\n");
} elsif(length($haircolour) < 1){
        &myerror("You didn't put in your hair colour.\n");
} elsif(length($eyecolour) < 1){
        &myerror("You didn't put in your eye colour.\n");
} elsif(length($cgi{'regstat'}) < 1){
        &myerror("You didn't say that you have paid or not.\n");
} elsif(($cgi{'regstat'}=~/paid/) && ( length($ref) < 1)){
        &myerror("You didn't put the reference number.\n");
} elsif(length($education) < 1){
        &myerror("You didn't put in your education.\n");
} elsif(length($occupation) < 1){
        &myerror("You didn't put in your occupation.\n");
} elsif(length($hobbies) < 1){
        &myerror("You didn't put in your hobbies.\n");
} elsif(length($foods) < 1){
        &myerror("You didn't put in your favourite foods.\n");
} elsif(length($films) < 1){
        &myerror("You didn't put in your favourite films.\n");
} elsif(length($vacations) < 1){
        &myerror("You didn't put in your favourite vacation spot.\n");
} elsif(length($music) < 1){
        &myerror("You didn't put in your favourite music.\n");
} elsif(length($tv) < 1){
        &myerror("You didn't put in your favourite TV shows.\n");
} elsif(length($books) < 1){
        &myerror("You didn't put in your favourite books.\n");
} elsif(length($sports) < 1){
        &myerror("You didn't put in your favourite sports.\n");
} elsif(length($pastimes) < 1){
        &myerror("You didn't put in your favourite pastimes.\n");
} elsif(length($email) < 1){
        &myerror("You didn't put in your e-mail address.\n");
} elsif(length($comments) < 1){
        &myerror("You didn't put in a description of yourself.\n");
} elsif(length($fullname) < 1){
        &myerror("You didn't put in your real name.\n");
} elsif(length($address) < 1){
        &myerror("You didn't put in your address.\n");
} elsif(length($phone) < 1){
        &myerror("You didn't put in your daytime phone number.\n");
} elsif($emailflag == 0){
	&myerror("You didn't put the right e-mail address format!\n");
}
$newflag = 1;
$kkadd = 1;
&goadd unless $ref;
if( $cgi{'regstat'} =~ /not/){
	$newflag = 0;
	$kkadd = 0;
}
if($kkadd == 1){
	@ss = split(/\./,$ref);
	if(length(@ss[0]) != 9){
		$newflag = 0;
		&myerror("You didn't put your right reference number.\n");
	}	
}
sub myerror {
        $errstr = shift @_;
        printf("%s", &PrintHeader);
        print <<EOF;
<h1>Um, there was something I didn't like</h1>
<p>
$errstr
<p>
Please use the Back button in your Web browser to
return to your form, fix this problem, and resubmit
your form.  
<p>
Thank you.
EOF

        exit;
}

printf("%s", &PrintHeader);
printf("<BODY bgcolor=\"#000000\" text=\"#00ffff\" link=\"#ffff00\"> \n");
printf("<center><img src=\"http://www.tlcplus.com/byline.gif\"></center> \n");
printf("<H3> \n");
printf("Thank you for filling out the form.\n");
printf("<BR>\n");
if($cgi{'regstat'}=~/notyet/){
	printf("You have not paid. We will put your profile up but your access is limited to make the payment now<A HREF=\"/tlcplus.html\">Click here</A>\n");
	open(FF,">>/var/spool/news/htdocs/tlcplus/notpaid.html");
	printf(FF "$email\n");
	close(FF);
}
printf("</H3> \n");
printf("<center><img src=\"http://www.tlcplus.com/byline.gif\"></center> \n");
printf("<i>The information that will be published in the directory is:</i><p> \n");
printf("<b>First Name:</b> $cgi{'nickname'}<br> \n");
printf("<b>E-Mail:</b> $cgi{'email'}<br> \n");
printf("<b>Age:</b> $cgi{'age'}<br> \n");
printf("<b>Weight:</b> $cgi{'weight'}<br> \n");
printf("<b>Height:</b> $cgi{'height'}<br> \n");
printf("<b>Hair Colour:</b> $cgi{'haircolour'}<br> \n");
printf("<b>Eye Colour:</b> $cgi{'eyecolour'}<br> \n");
printf("<b>Education:</b> $cgi{'education'}<br> \n");
printf("<b>Occupation:</b> $cgi{'occupation'}<br> \n");
printf("<b>Hobbies:</b> $cgi{'hobbies'}<br> \n");
printf("<b>Favourite Foods:</b> $cgi{'foods'}<br> \n");
printf("<b>Favourite Films:</b> $cgi{'films'}<br> \n");
printf("<b>Favourite Vacation Spots:</b> $cgi{'vacations'}<br> \n");
printf("<b>Favourite Music:</b> $cgi{'music'}<br> \n");
printf("<b>Favourite TV:</b> $cgi{'tv'}<br> \n");
printf("<b>Favourite Books:</b> $cgi{'books'}<br> \n");
printf("<b>Favourite Sports:</b> $cgi{'sports'}<br> \n");
printf("<b>Favourite Pastimes:</b> $cgi{'pastimes'}<br> \n");
printf("<b>City:</b> $cgi{'city'}<BR> \n");
printf("<b>Country:</b> $cgi{'country'}<p> \n");
printf("<b>Description of Yourself:</b> $cgi{'comments'}<p><br> \n");
printf("<center><img src=\"http://www.tlcplus.com/byline.gif\"></center> \n");
$savefile = join('',$homedir_tobesave,"/",$fil);
open(MAIL, "| /usr/bin/mail -s 'TLCPLUS Registration Form' john\@ezenet.com");
printf(MAIL "Subject: file name: $fil Stat: $cgi{'regstat'} $ref\n");
printf(MAIL "A TLCPLUS Registration has been filled out, as follows:\n\n");
printf(MAIL "================================================\n\n");
printf(MAIL "Full Name: $cgi{'fullname'} \n");
printf(MAIL "Nickname: $cgi{'nickname'} \n");
printf(MAIL "Sex: $cgi{'sex'} \n");
printf(MAIL "Seeking: $cgi{'seeking'} \n");
printf(MAIL "E-Mail: $cgi{'email'} \n");
printf(MAIL "Address: $cgi{'address'} \n");
printf(MAIL "Daytime Phone: $cgi{'phone'} \n");
printf(MAIL "Comments: $cgi{'comments'} \n");
printf(MAIL "------------------------------------------------\n\n");
printf(MAIL "This E-Form (c) Copyright 1996, Processing Intelligence Corp\n");
printf(MAIL "------------------------------------------------\n\n");
close(MAIL);
open(M, "| /usr/bin/mail -s 'TLCPLUS Registration Form' clinton\@ezenet.com");
printf(M "Subject: file name: $fil Stat: $cgi{'regstat'} $ref\n");
printf(M "A TLCPLUS Registration has been filled out, as follows:\n\n");
printf(M "================================================\n\n");
printf(M "Full Name: $cgi{'fullname'} \n");
printf(M "Nickname: $cgi{'nickname'}\n");
printf(M "Sex: $cgi{'sex'} \n");
printf(M "Seeking: $cgi{'seeking'} \n");
printf(M "E-Mail: $cgi{'email'} \n");
printf(M "Address: $cgi{'address'} \n");
printf(M "Daytime Phone: $cgi{'phone'} \n");
printf(M "Comments: $cgi{'comments'} \n");
printf(M "------------------------------------------------\n\n");
printf(M "This E-Form (c) Copyright 1996, Processing Intelligence Corp\n");
printf(M "------------------------------------------------\n\n");
close(M);
@newtime = localtime(time);
$news = (@newtime[5]*10000)+(@newtime[4]*100)+@newtime[3];
open(M, ">>reg_client.db");
printf(M "$cgi{'regstat'}:$cgi{'nickname'}:$cgi{'email'}:$fil:$news:$sex\n");
close(M);
open(SM, "| /usr/bin/mail -s 'TLCPLUS Registration Form' $email");
open(F,"/var/spool/news/htdocs/tlcplus/returnprof.html");
while(<F>){
	printf(SM "$_");
}
close(F);
close(SM);
open(MAIL,">$savefile");
printf(MAIL "<html><head><title>$cgi{'nickname'}</title></head> \n");
printf(MAIL "<body bgcolor=\"#000000\" text=\"#00ffff\" link=\"#ffff00\"> \n");
printf(MAIL "<center><img src=\"http://www.tlcplus.com/tlc_pic.jpg\"> \n");
printf(MAIL "<font size=4><b> $cgi{'nickname'}</b>&nbsp;&nbsp;From $cgi{'city'},$cgi{'country'}</font> \n");
printf(MAIL "<p><img src=\"http://www.tlcplus.com/byline.gif\"><p><table align=center width=600> \n");
printf(MAIL "<tr><td><b>AGE: $cgi{'age'}</b></td><td><b>EAT: $cgi{'foods'}</b></td></tr> \n");
printf(MAIL "<tr><td><b>WEIGHT: $cgi{'weight'}</b></td><td><b>FILMS: <b>$cgi{'films'}</b></td></tr> \n");
printf(MAIL "<tr><td><b>HEIGHT: $cgi{'height'}</b></td><td><b>VACATION: <b>$cgi{'vacations'}</b></td></tr> \n");
printf(MAIL "<tr><td><b>HAIR: $cgi{'haircolour'}</b></td><td><b>MUSIC: <b>$cgi{'music'}</b></td></tr> \n");
printf(MAIL "<tr><td><b>EYES: $cgi{'eyecolour'}</b></td><td><b>TV: <b>$cgi{'tv'}</b></td></tr> \n");
printf(MAIL "<tr><td><b>EDUCATION: $cgi{'education'}</b></td><td><b>BOOKS: <b>$cgi{'books'}</b></td></tr> \n");
printf(MAIL "<tr><td><b>OCCUPATION: $cgi{'occupation'}</b></td><td><b>SPORTS: <b>$cgi{'sports'}</b></td></tr> \n");
printf(MAIL "<tr><td><b>HOBBIES: $cgi{'hobbies'}</b></td><td><b>PASTIMES: <b>$cgi{'pastimes'}</b></td> \n");
printf(MAIL "<tr><td><b>City: $cgi{'city'}</b></td><td><b>Country: <b>$cgi{'country'}</b></td> \n");
printf(MAIL "</table><p><font size=4><center>\n");
if($cgi{'regstat'} =~ /paid/){
printf(MAIL "<b>E-MAIL: <a href=\"mailto:$cgi{'email'}\">$cgi{'email'}</a></center> \n");
}else{
printf(MAIL "<A HREF=\"/tlcplus.html\">E-MAIL ME FOR MORE INFORMATION</A></CENTER>\n");
}
printf(MAIL "<p><table align=center width=600 border=0><font size=4><b>More About <b>$cgi{'nickname'}</b>:<br> \n");
printf(MAIL "<tr><td><b><font size=4>$cgi{'comments'}</b></td></tr></font></table><p><br clear=\"all\"> \n");
open(NEXT,"tail.file");
while(<NEXT>)
{	
	printf(MAIL "$_");
}
close(NEXT);
close(MAIL);
$file = join('',"/var/spool/news/htdocs/tlcplus/",$sex,$seeking,".html");
$file =~ tr/A-Z/a-z/;
#printf("$file");
open(DB,">>$file");
printf(DB "$nickname:$age:$email\n");
close(DB);
if(($sex=~/Male/)&&($seeking=~/Male/)&&($newflag == 1)){
       $data="/var/spool/news/htdocs/tlcplus/men_men.htm";
       $fcount = "pmm.cnt";
}elsif(($sex=~/Male/)&&($seeking=~/Female/)&&($newflag == 1)){
       $data="/var/spool/news/htdocs/tlcplus/tlc_fmen.html";
       $fcount="pmf.cnt";
}elsif(($sex=~/Female/)&&($seeking=~/Female/)&&($newflag == 1)){
       $data="/var/spool/news/htdocs/tlcplus/wom_wom.htm";
       $fcount="pff.cnt";
}elsif(($sex=~/Female/)&&($seeking=~/Male/)&&($newflag == 1)){
       $data="/var/spool/news/htdocs/tlcplus/tlc_fwom.html";
       $fcount="pfm.cnt";
}elsif(($sex=~/Male/)&&($seeking=~/Male/)&&($newflag == 0)){
       $data="/var/spool/news/htdocs/tlcplus/men_me1.htm";
       $fcount = "mm.cnt";
}elsif(($sex=~/Male/)&&($seeking=~/Female/)&&($newflag == 0)){
       $data="/var/spool/news/htdocs/tlcplus/tlc_fme1.html";
       $fcount="mf.cnt";
}elsif(($sex=~/Female/)&&($seeking=~/Female/)&&($newflag == 0)){
       $data="/var/spool/news/htdocs/tlcplus/wom_wo1.htm";
       $fcount="ff.cnt";
}elsif(($sex=~/Female/)&&($seeking=~/Male/)&&($newflag == 0)){
       $data="/var/spool/news/htdocs/tlcplus/tlc_fwo1.html";
       $fcount="fm.cnt";
}
open(F,$fcount);
while(<F>){
       $count = $_;
}
close(F);
open(F,">$fcount");
if($count==4){
       print F "0";
}else{
       print F $count+1;
}
close(F);
$flag=0;
$nickname=~tr/a-z/A-Z/;
open(Fin,$data);
open(Fout,">tmp.html");
while(<Fin>){
       $line = $_;
    if($flag==0){
       if($line =~ /^<\/TABLE>/){
               if($count==4){
                       print Fout "<TR>\n";
                       print Fout "<TD><IMG SRC=\"tlc_small.gif\">\n";
                       print Fout "<TD><A HREF=\"$fil\">$nickname</A>\n";
               }else{
                       print Fout "<TD><A HREF=\"$fil\">$nickname</A>\n";
               }
	       $flag=1;
	}
    }
       print Fout $line;
}
close(Fout);
close(Fin);
$command = join('',"cp tmp.html ",$data);
system($command);
sub goadd{
	$kkadd = 0;
	$newflag = 0;
}
